<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/7
 * Time: 15:07
 */

namespace app\api\controller;


use think\Db;

class Information extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 发布风讯
     * @return \think\response\Json
     * info_show     可见人群   1 公开  2 粉丝  3 好友  4 本人可见
     * info_friend_user    可见的好友
     * info_is_publish    1 草稿箱   2 发布
     * info_content   发布内容
     * info_images    图片/视频
     * info_type   1 文字  2 图片文字   3 视频文字
     * info_addr      发布的地址
     * info_lon     经度
     * info_lat     纬度
     */
    public function publishInformation(){
        $params = input('');
        $insert = [
            'info_user_id' => $this->user_id,
//            'info_show' => $params['info_show'],
            'info_is_publish' => $params['info_is_publish'],
            'info_time' => $this->create_time
        ];
        if(isset($params['info_content']) && !empty($params['info_content'])){
            $insert['info_content'] = $params['info_content'];
        }
        if(isset($params['info_images']) && !empty($params['info_images'])){
            $insert['info_images'] = $params['info_images'];
        }
        if(isset($params['info_type']) && !empty($params['info_type'])){
            $insert['info_type'] = $params['info_type'];
        }
        if(isset($params['info_show']) && !empty($params['info_show'])){
            $insert['info_show'] = $params['info_show'];
        }
        if(isset($params['info_addr']) && !empty($params['info_addr'])){
            $insert['info_addr'] = $params['info_addr'];
            $insert['info_lon'] = $params['info_lon'];
            $insert['info_lat'] = $params['info_lat'];
        }
        if($params['info_show'] == 3){
            $insert['info_friend_user'] = ',' . $params['info_friend_user'] . ',';
        }
        $res = Db::table('up_information')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 推荐风讯列表
     * @return \think\response\Json
     * page   页码
     */
    public function informationList(){
//        $where['info_is_publish'] = 2; // 已发布的
//        $where['info_user_id'] = $this->user_id; // 自己的
//        $where['info_status'] = 1;
        // 我是谁的粉丝
        $fans = Db::table('up_like')
            ->join('up_u_user', 'like_by_user_id = user_id', 'left')
            ->where('like_user_id', $this->user_id)
            ->where('user_status', 1)
            ->column('like_by_user_id');
        // 我添加的好友
//        $my_friend = Db::table('up_friend')
//            ->join('up_u_user', 'f_friend_id = user_id', 'left')
//            ->where('f_my_id', $this->user_id)
//            ->where('f_status', 2)
//            ->where('user_status', 1)
//            ->column('f_friend_id');
        // 谁添加了我为好友
//        $add_my = Db::table('up_friend')
//            ->join('up_u_user', 'f_my_id = user_id', 'left')
//            ->where('f_friend_id', $this->user_id)
//            ->where('f_status', 2)
//            ->where('user_status', 1)
//            ->column('f_my_id');
        // 我的好友
//        $friends = array_unique(array_merge($my_friend, $add_my));
        $list = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'left')
            ->field('user_id, user_username, user_image, info_content, info_images, info_type, 
            info_addr, info_read_num, info_time, info_id, info_status, info_link')
//            ->where($where)
            ->whereOr(function ($query) use ($fans){
                $query->where('info_user_id', 'IN', $fans)
                    ->where('info_show', 2)
                    ->where('info_is_publish', 2)
                    ->where('info_status', 1);
            })
            ->whereOr(function ($query1){
                $query1->where('info_friend_user', 'like', '%,' . $this->user_id . ',%')
                    ->where('info_show', 3)
                    ->where('info_is_publish', 2)
                    ->where('info_status', 1);
            })
            ->whereOr(function ($query2){
                $query2->where('info_user_id', 'NEQ', $this->user_id)
                    ->where('info_show', 1)
                    ->where('info_is_publish', 2)
                    ->where('info_status', 1);
            })
            ->whereOr(function ($query3){
                $query3->where('info_user_id', $this->user_id)
                    ->where('info_is_publish', 2)
                    ->where('info_status', 1);
            })
            ->order('info_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            foreach($list as $k => $v){
                // 图片
                if($v['info_images']){
                    $list[$k]['info_images'] = explode(',', trim($v['info_images'], ','));
                }else{
                    $list[$k]['info_images'] = [];
                }
                // 关注
                $is_like = $this->isLike($v['user_id']);
                $list[$k]['is_like'] = $is_like; // 1 已关注   2 未关注
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 风讯详情
     * @return \think\response\Json
     * info_id  风讯ID
     */
    public function informationDetail(){
        header("Access-Control-Allow-Origin: *");
        $info_id = input('info_id');
        // 浏览量+1
        Db::table('up_information')->where('info_id', $info_id)->setInc('info_read_num');
        $detail = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'left')
            ->field('user_id, user_image, user_username, info_content, info_images, info_type, 
            info_addr, info_time, info_id, info_read_num, info_link')
            ->where('info_id', $info_id)
            ->find();
        
        if($detail){
            // 图片
            if($detail['info_images']){
                $detail['info_images'] = explode(',', trim($detail['info_images'], ','));
            }else{
                $detail['info_images'] = [];
            }
            // 关注
            $is_like = $this->isLike($detail['user_id']);
            $detail['is_like'] = $is_like; // 1 已关注   2 未关注
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 风讯详情页评论
     * @return \think\response\Json
     * page     页码
     * info_id  风讯ID
     */
    public function informationCommentDetail(){
        header("Access-Control-Allow-Origin: *");
        $info_id = input('info_id');
        // 评论
        $comment = $this->info_comment($info_id, 1);
        if($comment){
            // 评论+回复数量
            $comment_ids = $this->info_comment($info_id, 2);
            $comment_num = count($comment_ids);
            $reply_num = $this->info_reply($comment_ids, 3);
            $info_comment_num = $comment_num + $reply_num;
            foreach($comment as $k => $v){
                // 平台评论
                if($v['comment_from'] == 2){
                    $comment[$k]['user_username'] = '官方评论';
                    $comment[$k]['user_image'] = Db::table('up_other')
                        ->where('other_id', 4)
                        ->value('other_logo');
                }
                // 回复数量
                $comment[$k]['comment_reply_num'] = $this->info_reply($v['comment_id'], 3);
                // 回复
                $reply = $this->info_reply($v['comment_id'], 1);
                if($reply){
                    // 回复的是评论还是回复
                    foreach($reply as $kk => $vv){
                        // 平台回复
                        if($vv['reply_from'] == 2){
                            $reply[$kk]['user_username'] = '官方回复';
                            $reply[$kk]['user_image'] = Db::table('up_other')
                                ->where('other_id', 4)
                                ->value('other_logo');
                        }
                        if($vv['reply_type'] == 2){
                            // 回复的是谁
                            $to_user = Db::table('up_information_reply')
                                ->join('up_u_user', 'user_id = reply_user_id', 'left')
                                ->where('reply_id', $vv['reply_reply_id'])
                                ->field('user_username, reply_from')
                                ->find();
                            if($to_user['reply_from'] == 1){
                                $reply[$kk]['to_user_name'] = $to_user['user_username'];
                            }else{
                                $reply[$kk]['to_user_name'] = '官方回复';
                            }
                        }
                    }
                    $comment[$k]['reply'] = $reply;
                }else{
                    $comment[$k]['reply'] = [];
                }
            }
            return json(['status' => 200, 'msg' => $comment, 'info_comment_num' => $info_comment_num]);
        }else{
            return json(['status' => 200, 'msg' => [], 'info_comment_num' => 0]);
        }
    }
    
    /**
     * 推荐关注
     * @return \think\response\Json
     * range     调用场景   1 进入关注页面   2 查看更多
     * page      页码
     */
    public function recommendUser(){
        $range = input('range'); // 1 进入关注页,展示5个   2 查看更多
        // 推荐用户数组
        $recommend = Db::table('up_u_user')
            ->where('user_status', 1)
            ->where('user_is_tuijian_like', 2)
            ->where('user_id', 'NEQ', $this->user_id)
            ->column('user_id');
        // 我关注的用户
        $my_like = Db::table('up_like')
            ->join('up_u_user', 'user_id = like_by_user_id', 'left')
            ->where('like_user_id', $this->user_id)
            ->where('user_status', 1)
            ->column('like_by_user_id');
        
        if(empty($recommend)){
            $list = [];
        }else{
            // 判断推荐用户是不是都是我的关注用户
            $flag = 1;
            foreach($recommend as $k => $v){
                if(!in_array($v, $my_like)){
                    $flag = 2;
                }
            }
            if($flag == 1){ // 如果所有有推荐的用户都是我的关注的用户
                $list = [];
            }else{ // 取出不是我的关注的推荐用户
                $same = array_intersect($recommend, $my_like);
                $diff = array_diff($recommend, $same);
                $list = Db::table('up_u_user')
                    ->where('user_id', 'IN', $diff)
                    ->order('user_time desc')
                    ->field('user_id, user_username, user_image, user_intro');
                if($range == 1){
                    $list->limit(5);
                }else{
                    $list->page($this->page, 10);
                }
                $list = $list->select();
            }
        }
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 举报原因列表
     * @return \think\response\Json
     */
    public function reportReason(){
        $type = Db::table('up_report_reason')->field('rr_id, rr_name')->order('rr_time desc')->select();
        return json(['status' => 200, 'msg' => $type]);
    }
    
    /**
     * 提交举报
     * @return \think\response\Json
     * report_reason_id      举报原因ID,其它传0
     * report_reason         举报原因
     * obj_id                风讯/评论/回复ID
     * report_type           举报类型   1 风讯   2 评论   3 回复
     */
    public function doReport(){
        $report_reason_id = input('report_reason_id'); // 其他传0
        $report_reason = input('report_reason');
        $obj_id = input('obj_id'); // 风讯/评论/回复ID
        $report_type =  input('report_type'); // 举报类型   1 风讯  2 评论  3 回复
        
        if($report_type == 1){
            $exist = Db::table('up_report')
                ->where('report_user_id', $this->user_id)
                ->where('report_info_id', $obj_id)
                ->find();
            $to_user_id = Db::table('up_information')
                ->where('info_id', $obj_id)
                ->value('info_user_id');
        }elseif($report_type == 2){
            $exist = Db::table('up_report')
                ->where('report_user_id', $this->user_id)
                ->where('report_comment_id', $obj_id)
                ->find();
            $to_user_id = Db::table('up_information_comment')
                ->where('comment_id', $obj_id)
                ->value('comment_user_id');
        }else{
            $exist = Db::table('up_report')
                ->where('report_user_id', $this->user_id)
                ->where('report_reply_id', $obj_id)
                ->find();
            $to_user_id = Db::table('up_information_reply')
                ->where('reply_id', $obj_id)
                ->value('reply_user_id');
        }
        if($exist) return json(['status' => 0, 'msg' => '已收到您的举报，请勿重新提交']);
        
        if($report_reason_id != 0){
            $report_reason = Db::table('up_report_reason')
                ->where('rr_id', $report_reason_id)
                ->value('rr_name');
        }
        $insert = [
            'report_user_id' => $this->user_id,
            'report_to_user_id' => $to_user_id,
            'report_reason_id' => $report_reason_id,
            'report_reason' => $report_reason,
            'report_time' => $this->create_time,
            'report_type' => $report_type
        ];
        if($report_type == 1){
            $insert['report_info_id'] = $obj_id;
        }elseif($report_type == 2) {
            $insert['report_comment_id'] = $obj_id;
        }else{
            $insert['report_reply_id'] = $obj_id;
        }
        $res = Db::table('up_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 评论风讯
     * @return \think\response\Json
     * comment_info_id     被评论的风讯ID
     * comment_content     评论的内容
     */
    public function commentInfo(){
        $comment_info_id = input('comment_info_id');
        $comment_content = input('comment_content');
        $comment = [
            'comment_user_id' => $this->user_id,
            'comment_info_id' => $comment_info_id,
            'comment_content' => $comment_content,
            'comment_from' => 1,
            'comment_time' => $this->create_time
        ];
        $res = Db::table('up_information_comment')->insert($comment);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 回复
     * @return \think\response\Json
     * reply_type    回复类型   1 回复评论   2 回复的回复
     * reply_comment_id   评论的ID
     * reply_content      回复内容
     * reply_reply_id     回复的回复ID，如果回复的是评论，则传0
     */
    public function reply(){
        $reply_type = input('reply_type'); // 1 评论的回复   2 回复的回复
        $reply_comment_id = input('reply_comment_id');
        $reply_content = input('reply_content');
        $reply_reply_id = input('reply_reply_id'); // 如果回复的是评论，则传0
        $reply= [
            'reply_type' => $reply_type,
            'reply_user_id' => $this->user_id,
            'reply_comment_id' => $reply_comment_id,
            'reply_reply_id' => $reply_reply_id,
            'reply_content' => $reply_content,
            'reply_from' => 1,
            'reply_time' => $this->create_time
        ];
        $res = Db::table('up_information_reply')->insert($reply);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 删除评论
     * @return \think\response\Json
     * comment_id    评论ID
     */
    public function delComment(){
        $comment_id = input('comment_id');
        $res = Db::table('up_information_comment')->where('comment_id', $comment_id)->setField('comment_status', 2);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 删除回复
     * @return \think\response\Json
     * reply_id     回复ID
     */
    public function delReply(){
        $reply_id = input('reply_id');
        $res = Db::table('up_information_reply')->where('reply_id', $reply_id)->setField('reply_status', 2);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 评论详情
     * @return \think\response\Json
     * comment_id    评论ID
     */
    public function commentDetail(){
        $comment_id = input('comment_id');
        $comment = Db::table('up_information_comment')
            ->join('up_u_user', 'user_id = comment_user_id', 'left')
            ->where('comment_id', $comment_id)
            ->field('comment_user_id, user_username, user_image, comment_id, comment_info_id, 
            comment_content, comment_from, comment_time')
            ->find();
        if($comment){
            if($comment['comment_from'] == 2){
                $comment['user_username'] = '官方评论';
                $comment['user_image'] = Db::table('up_other')
                    ->where('other_id', 4)
                    ->value('other_logo');
            }
            $reply = $this->info_reply($comment['comment_id'], 2);
            if($reply){
                // 回复的是评论还是回复
                foreach($reply as $k => $v){
                    // 平台回复
                    if($v['reply_from'] == 2){
                        $reply[$k]['user_username'] = '官方回复';
                        $reply[$k]['user_image'] = Db::table('up_other')
                            ->where('other_id', 4)
                            ->value('other_logo');
                    }
                    if($v['reply_type'] == 2){
                        // 回复的是谁
                        $to_user = Db::table('up_information_reply')
                            ->join('up_u_user', 'user_id = reply_user_id', 'left')
                            ->where('reply_id', $v['reply_reply_id'])
                            ->field('user_username, reply_from')
                            ->find();
                        if($to_user['reply_from'] == 1){
                            $reply[$k]['to_user_name'] = $to_user['user_username'];
                        }else{
                            $reply[$k]['to_user_name'] = '官方回复';
                        }
                    }
                }
                $comment['reply'] = $reply;
            }else{
                $comment['reply'] = [];
            }
            return json(['status' => 200, 'msg' => $comment]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我是否关注了某人
     * @param $other_user_id   其他人ID
     * @return int
     */
    private function isLike($other_user_id){
        $is_like = Db::table('up_like')
            ->where('like_user_id', $this->user_id)
            ->where('like_by_user_id', $other_user_id)
            ->find();
        if($is_like){
            $like = 1;
        }else{
            $like = 2;
        }
        return $like;
    }
    
    /**
     * 评论
     * @param $info_id     风讯ID
     * @param $range       1 列表调用   2 数量调用
     * @return $this|array|false|\PDOStatement|string|\think\Collection
     */
    private function info_comment($info_id, $range){
        $comment = Db::table('up_information_comment')
            ->join('up_u_user', 'user_id = comment_user_id', 'left')
            ->where('comment_status', 1)
            ->where('comment_info_id', $info_id);
        if($range == 1){ // 列表
            $comment = $comment->field('comment_user_id, user_username, user_image, comment_id, comment_info_id, 
                comment_content, comment_from, comment_time')
                ->page($this->page, 10)
                ->order('comment_time desc')
                ->select();
        }else{ // ID数组
            $comment = $comment->column('comment_id');
        }
        return $comment;
    }
    
    /**
     * 回复
     * @param $comment_id     评论ID
     * @param $range          1 风讯详情列表   2 评论详情列表   3 回复数量
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function info_reply($comment_id, $range){
        $reply = Db::table('up_information_reply')
            ->join('up_u_user', 'user_id = reply_user_id', 'left')
            ->where('reply_status', 1);
        if($range == 1 || $range == 2){ // 列表
            $reply->field('reply_id, reply_type, reply_user_id, reply_comment_id, reply_reply_id, 
                    reply_content, reply_from, reply_time, user_username, user_image')
                ->where('reply_comment_id', $comment_id)
                ->order('reply_time desc');
        }
        if($range == 1){
            $reply = $reply->limit(2)->select();
        }elseif($range == 2){
            $reply = $reply->page($this->page, 10)->select();
        }else{
            $reply = $reply->where('reply_comment_id', 'IN', $comment_id)->count();
        }
        return $reply;
    }
}